package org.six.background.service.impl;

import org.six.background.mapper.UserMapper;
import org.six.background.pojo.User;
import org.six.background.service.AccountService;
import org.six.background.util.UploadUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class AccountServiceImpl implements AccountService {

    @Autowired(required = false)
    private UserMapper userMapper;

    @Autowired(required = false)
    private UploadUtil uploadUtil;


    private final String dirname="/accountimg";

    @Override
    public int count() {
        int count = userMapper.countByExample(null);
        return count;
    }

    @Override
    public Map userAll(int page, int pagesize) {
        int count = count();
        double pageMax=Math.ceil(count/pagesize)+1;
        page=page>(int)(pageMax)?(int)(pageMax):page;
        Map pages=new HashMap();
        pages.put("page",page);
        pages.put("pagesize",pagesize);
        pages.put("count",count);
        pages.put("pagemax",(int)pageMax);

        Map map1=new HashMap();
        map1.put("index",(page-1)*pagesize);
        map1.put("pagesize",pagesize);

        List<User> users = userMapper.userAll(map1);

        Map map=new HashMap();
        map.put("users",users);
        map.put("pages",pages);
        return map;
    }

    @Override
    public String insert(User user) {
        User user1=new User();
        user1.setUsname(user.getUsname());
        user1.setUspass(user.getUspass());
        user1.setUscard(user.getUscard());
        user1.setUsphone(user.getUsphone());
        user1.setUsimg("https://cube.elemecdn.com/3/7c/3ea6beec64369c2642b92c6726f1epng.png");
        user1.setUstype(0);
        int i = userMapper.insert(user1);
        if (i>0){
            return "ok";
        }else {
            return "no";
        }
    }


    //编辑账户
    @Override
    public String updateus(int usid,String usname,String usphone,String uscard) {
        System.out.println("账户"+usid+" "+usname+" "+uscard+" "+usphone);
        User user=new User();
        user.setUsid(usid);
        user.setUsname(usname);
        user.setUsphone(usphone);
        user.setUscard(uscard);
        userMapper.updateByPrimaryKeySelective(user);
        return "us";
    }

    //修改头像
    @Override
    public String updatetp(MultipartFile file, HttpServletRequest request){
        String url = uploadUtil.upload(file, request, dirname);
        return url;
    }

    ////第一步，在页面加载前传送图片地址
    @Override
    public String toux(int usid) {
        User user = userMapper.selectByPrimaryKey(usid);
        System.out.println(user.getUsimg());
        return user.getUsimg();
    }

    //如果有头像，修改密码和头像
    @Override
    public String uspass(HttpServletRequest request,int usid,String uspass, String newurl, String oldurl) {
        System.out.println("进入uspass");
        //uploadUtil.delete(oldurl,request,dirname);
        User user=new User();
        user.setUsid(usid);
        user.setUspass(uspass);
        System.out.println("uspass"+uspass);
        if(newurl!=null){
            user.setUsimg(newurl);
            System.out.println(user.getUsimg());
        }
        userMapper.updateByPrimaryKeySelective(user);
        return "ok";
    }

//    User user=new User();
//    user.setUsimg(newurl);
//    System.out.println("密码为空"+user.getUsimg());
//    userMapper.updateByPrimaryKeySelective(user);

    //仅根据id修改头像
    @Override
    public String uspass(HttpServletRequest request,int usid, String newurl, String oldurl) {
        System.out.println("进入uspass2");
        //uploadUtil.delete(oldurl,request,dirname);
        User user=new User();
        user.setUsid(usid);
        if(newurl!=null){
            user.setUsimg(newurl);
            System.out.println(user.getUsimg());
        }
        userMapper.updateByPrimaryKeySelective(user);
        return "ok";
    }

    //根据id修改密码
    @Override
    public String uspass(int usid, String uspass) {
        User user=new User();
        user.setUsid(usid);
        user.setUspass(uspass);
        System.out.println(uspass);
        userMapper.updateByPrimaryKeySelective(user);
        return "okk";
    }

}
